package ckw.gui;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import core.exceptions.NoElectionResultsException;

import ckw.database.DatabaseManager;
import ckw.database.Election;
import ckw.database.resultPattern.ElectionResultPattern;
import ckw.database.resultPattern.VotingCardResultPattern;
import ckw.database.resultPattern.VotingSubcardOptionResultPattern;

public class DlgElectionResults extends Dialog {
	private ElectionResultPattern electionResultPattern;
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public DlgElectionResults(Shell parentShell, ElectionResultPattern electionResultPattern) {
		super(parentShell);
		setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE);
		
		this.electionResultPattern = electionResultPattern;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) { // TODO: rozważyć zapis do bazy
		Composite container = (Composite) super.createDialogArea(parent);
		FillLayout fl_container = new FillLayout(SWT.HORIZONTAL);
		fl_container.marginWidth = 10;
		fl_container.marginHeight = 10;
		container.setLayout(fl_container);
		
		Browser browser = new Browser(container, SWT.NONE);
		
		StringBuilder html = new StringBuilder();
		
		html.append("<html><body><p style=\"text-align: center;\"><strong>");
		html.append(electionResultPattern.getName());
		html.append("</strong></p><p style=\"text-align: center;\">&nbsp;</p><p><em>Data rozpoczęcia: ");
		html.append(electionResultPattern.getStartDate().toString());
		html.append("<br />Data zakończenia: ");
		html.append(electionResultPattern.getEndDate());
		html.append("<br />Uprawnionych do głosowania: ");
		html.append(electionResultPattern.getVoters());
		html.append("<br />Zagłosowało: ");
		html.append(electionResultPattern.getVotes());
		html.append("<br />Frekwencja: ");
		html.append((electionResultPattern.getVotes()*100)/electionResultPattern.getVoters());
		html.append("%<br />Ważnych głosów: ");
		html.append(electionResultPattern.getProperVotes());
		html.append("<br />Nieważnych głosów: ");
		html.append(electionResultPattern.getVotes()-electionResultPattern.getProperVotes());
		html.append("</em></p><ul>");
		for (VotingCardResultPattern votingCard : electionResultPattern.getVotingCards()) {
			html.append("<li><strong><em>");
			html.append(votingCard.getName());
			html.append("</em></strong><ol>");
			for (VotingSubcardOptionResultPattern option : votingCard.getSortedVotingSubcardOptions()){
				html.append("<li><em>");
				html.append(option.getName());
				html.append(" - ");
				html.append(option.getSubcardName());
				html.append("&nbsp;&nbsp;&nbsp;");
				html.append(option.getVotes());
				html.append("</em></li>");
			}
			html.append("</ol></li>");
		}
		html.append("</ul></body></html>");
				
		browser.setText(html.toString());
		
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}
}
